package com.suo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suo.mapper.ProductInfoMapper;
import com.suo.pojo.ProductInfo;
import com.suo.pojo.ProductInfoExample;
import com.suo.pojo.vo.ProductInfoVo;
import com.suo.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    private ProductInfoMapper mapper;

    @Autowired
    public void setMapper(ProductInfoMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public List<ProductInfo> getAll() {
        List<ProductInfo> productInfos = mapper.selectByExample(new ProductInfoExample());
        return productInfos;
    }

    @Override
    public PageInfo split(int pageNum, int pageSize) {
        //分页插件使用PageHelper工具类完成分页设置
        PageHelper.startPage(pageNum,pageSize);

        //进行条件查询，降序
        ProductInfoExample example = new ProductInfoExample();
        example.setOrderByClause("p_id desc");
        List<ProductInfo> productInfos = mapper.selectByExample(example);

        //将查询到的集合封装到PageInfo中
        PageInfo<ProductInfo> pageInfo = new PageInfo<>(productInfos);

        return pageInfo;
    }

    @Override
    public int save(ProductInfo info) {
        int insert = mapper.insert(info);

        return insert;
    }

    @Override
    public ProductInfo one(int pid) {
        ProductInfoExample example = new ProductInfoExample();
        example.createCriteria().andPIdEqualTo(pid);
        List<ProductInfo> productInfos = mapper.selectByExample(example);
        if(productInfos.size() > 0) {
            return productInfos.get(0);
        }
        else {
            return null;
        }
    }

    @Override
    public int update(ProductInfo info) {
        ProductInfoExample example = new ProductInfoExample();
        ProductInfoExample.Criteria criteria = example.createCriteria();
        criteria.andPIdEqualTo(info.getpId());
        int i = mapper.updateByExampleSelective(info, example);
        return i;
    }

    @Override
    public int delete(int pid) {
        ProductInfoExample example = new ProductInfoExample();
        example.createCriteria().andPIdEqualTo(pid);
        int i = mapper.deleteByExample(example);
        return i;
    }

    @Override
    public int deletebatch(int[] str) {
        int i = mapper.deleteBatch(str);
        return  i;
    }

    @Override
    public List<ProductInfo> selectCondition(ProductInfoVo vo) {
        return mapper.selectCondition(vo);
    }

    @Override
    public PageInfo splitCondition(ProductInfoVo vo, int pageSize) {
        //分页插件使用PageHelper工具类完成分页设置
        PageHelper.startPage(vo.getPage(),pageSize);

        List<ProductInfo> productInfos = mapper.selectCondition(vo);

        //将查询到的集合封装到PageInfo中
        PageInfo<ProductInfo> pageInfo = new PageInfo<>(productInfos);

        return pageInfo;
    }


}
